package com.rerun.common.util;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * The type Validator.
 */
public class Validator {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";

    /**
     * 正则表达式：验证手机号
     */
   public static final String REGEX_MOBILE = "^[1][3,4,5,7,8][0-9]{9}$";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";

    /**
     * 正则表达式：验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

    /**
     * 正则表达式：验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式：验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    /**
     * 校验用户名
     *
     * @param username the username
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isUsername(String username) {
		return Pattern.matches(REGEX_USERNAME, username);
	}

    /**
     * 校验密码
     *
     * @param password the password
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isPassword(String password) {
		return Pattern.matches(REGEX_PASSWORD, password);
	}

    /**
     * 校验手机号
     *
     * @param mobile the mobile
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return  Pattern.matches(REGEX_MOBILE,mobile);
	}

    /**
     * 校验邮箱
     *
     * @param email the email
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isEmail(String email) {
		return Pattern.matches(REGEX_EMAIL, email);
	}

    /**
     * 校验汉字
     *
     * @param chinese the chinese
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isChinese(String chinese) {
		return Pattern.matches(REGEX_CHINESE, chinese);
	}

    /**
     * 校验身份证
     *
     * @param idCard the id card
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isIDCard(String idCard) {
		return Pattern.matches(REGEX_ID_CARD, idCard);
	}

    /**
     * 校验URL
     *
     * @param url the url
     * @return 校验通过返回true ，否则返回false
     */
    public static boolean isUrl(String url) {
		return Pattern.matches(REGEX_URL, url);
	}

    /**
     * 校验IP地址
     *
     * @param ipAddr the ip addr
     * @return boolean boolean
     */
    public static boolean isIPAddr(String ipAddr) {
		return Pattern.matches(REGEX_IP_ADDR, ipAddr);
	}


    /**
     * To string json map.
     *
     * @param result the result
     * @return the map
     */
    public static Map<String, Object> toStringJson(BindingResult result) {

        Map<String, Object> map = new HashMap<String, Object>();

        for (FieldError fieldError : result.getFieldErrors()) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        return map;
    }

    /**
     * 空值校验.
     *
     * @param <T> the type parameter
     * @param t   the t
     * @return the t
     * @throws IllegalAccessException the illegal access exception
     */
    public static <T> T haveNullField(T t) throws IllegalAccessException {
        Class clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object object = field.get(t);
            if (Objects.isNull(object)) {
                return t;
            }
        }
        return null;
    }

    public static <T> T defaultIfNull(T t, T defaultVal){
        if (t==null){
            return defaultVal;
        }
        return t;
    }
}
